package com.zhiyi.coco.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.zhiyi.coco.dao.SysLoginInfoMapper;
import com.zhiyi.coco.dao.SysResourceMapper;
import com.zhiyi.coco.dao.SysRoleMapper;
import com.zhiyi.coco.dao.SysUserMapper;
import com.zhiyi.coco.entity.SysUser;
import com.zhiyi.coco.service.SysUserService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Log4j
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysLoginInfoMapper loginInfoMapper;

    @Override
    public SysUser getInfoById(Integer id) {
        return userMapper.getInfoById(id);
    }

    @Override
    public Integer deleteInfoById(Integer id) {
        return userMapper.deleteInfoById(id);
    }

    @Override
    public Integer updateimgbyid(Integer id) {
        return loginInfoMapper.updateimgbyid(id);
    }

    @Override
    public Integer updateByPrimaryKeySelective(SysUser user) {
        Integer i=userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    @Override
    public Integer insertSelective(SysUser user) {
        Integer i=userMapper.insertSelective(user);
        return i;
    }

    @Override
    public Integer updateuId(Integer id) {
        return loginInfoMapper.updateuId(id);
    }

    @Override
    public List<SysUser> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysResourceMapper resourceMapper;

    @Override
    public SysUser login(SysUser user) {
        SysUser u = userMapper.getUserByName(user.getUsername());
        log.info(u.getUsername());
//        SecureUtil.md5(user.getPassword() + "abc")
        if (SecureUtil.md5(user.getPassword()+"abc").equals(u.getSalt())) {
            //通过id获取用户角色
            List<String> role = roleMapper.getNameByUserName(u.getId());
            List<String> resources = resourceMapper.getResourceByUserId(u.getId());
            u.setRoles(role);
            u.setResources(resources);
            return u;
        }
        return null;
    }


    @Override
    public SysUser loginByCode(String loginCode, String pwd) {
        SysUser u = userMapper.loginByCode(loginCode, pwd);
        return u;
    }

    @Override
    public SysUser loginByUser(SysUser user) {
        SysUser u = userMapper.getUserByName(user.getUsername());
//        SecureUtil.md5(user.getPassword() + "abc")
        if (u.getPassword().equals(SecureUtil.md5(user.getPassword()))) {
            //通过id获取用户角色
            List<String> role = roleMapper.getNameByUserName(u.getId());
            List<String> resources = resourceMapper.getResourceByUserId(u.getId());
            u.setRoles(role);
            u.setResources(resources);
            return u;
        }
        return null;
    }
}
